/*
 * Student Name: Philippe Gambling
 * Course: CSCI 5733
 * Assignment: Homework 1
 * Created: Jun 18, 2009
 * Modified: Jun 18, 2009
 * 
 * Class Name: MoreoverSource
 * Description: Retrieves and parses Moreover CSV files.
 */

package csci5733.hw1;

import java.io.BufferedReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Philippe Gambling
 */
public class MoreoverSource implements NewsSource {
    private static Hashtable<String,ArrayList> supportedTopics = new Hashtable();
    private NewsUrl url;

    private List newsItemList = new ArrayList();
    private String source = "moreover";

    public MoreoverSource(NewsUrl url) {
        this.url = url;

        // Map my topics to CNET's topics
        MoreoverSource.supportedTopics.put("software",
                new ArrayList<String>(Arrays.asList("database","java", "pc_software", "os")));
        MoreoverSource.supportedTopics.put("hardware",
                new ArrayList<String>(Arrays.asList("handhelds")));
        MoreoverSource.supportedTopics.put("security",
                new ArrayList<String>(Arrays.asList("security")));
        MoreoverSource.supportedTopics.put("network",
                new ArrayList<String>(Arrays.asList("handhelds")));
        MoreoverSource.supportedTopics.put("personal",
                new ArrayList<String>(Arrays.asList("personal")));
        MoreoverSource.supportedTopics.put("database",
                new ArrayList<String>(Arrays.asList("database")));
        MoreoverSource.supportedTopics.put("enterprise",
                new ArrayList<String>(Arrays.asList("enterprise")));
        MoreoverSource.supportedTopics.put("java",
                new ArrayList<String>(Arrays.asList("java")));
        MoreoverSource.supportedTopics.put("os",
                new ArrayList<String>(Arrays.asList("os")));
        MoreoverSource.supportedTopics.put("pc_software",
                new ArrayList<String>(Arrays.asList("pc_software")));
    }

    @Override
    public List fetchTopic(String topic) throws Exception {
        // Return an empty list if this isn't a known Moreover topic.
        if(!(MoreoverSource.supportedTopics.keySet().contains(topic))) {
            return new ArrayList();
         }

        Iterator<String> itr = MoreoverSource.supportedTopics.get(topic).iterator();
        while(itr.hasNext()) {
            // Parse the Moreover CSV for each sub topic
            String nextTopic = itr.next();
            parseCsv(url.query(nextTopic));
        }

        return newsItemList;
    }

    private void parseCsv(Reader reader) throws Exception {
        BufferedReader csv = new BufferedReader(reader);
        String line, delim = "#=#";
        NewsItem newItem;
        while((line = csv.readLine()) != null) {
            // If I remove all quotation marks first, I can't split line 
            // into fields because commas can be in the title. 
            // So I replace the "," divider with my own delimiter,
            // then remove quotation marks, and finally split into fields.
            line = line.replaceFirst("\\d\\d?,",""); // Remove the numbering
            line = line.replaceAll("\",\"",delim);
            line = line.replaceAll("\"", "");
            String[] info = line.split(delim);
            newItem = new NewsItem(this.source);
            newItem.setTitle(info[0]);
            newItem.setUrl(info[1]);
            newItem.setTime(info[2]);
            this.newsItemList.add(newItem);
        }
    }

}
